﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using SMSConsulting.SMSLIB.Utilities;
using Access.PowerTools.Settings;

namespace Access.PowerTools.TestWinForm
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Access.PowerTools.Settings.Globals.Init();
            Access.PowerTools.Settings.Globals.SimulationModeOn = true;

            SimpleLogWriter.LogFullPath =
                  Globals.LogFullPath;
            SimpleLogWriter.WriteLogLine(Globals.APPLICATION_TITLE + " - " + Globals.APPLICATION_VERSION + " - started.");

            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            Application.Run(new MainForm());

        }
        /// <summary>
        /// Glopbal (catch-all) error handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            Exception ex = e.Exception;
            string caption = Globals.APPLICATION_TITLE;
            string message = "";
            string message1 = "";

            if (ex is ApplicationException)
            {
                message = ex.Message;
                MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (ex is System.Data.OleDb.OleDbException)
            {
                message1 = ex.Message;
                MessageBox.Show(message1, caption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                message1 = ex.Message + System.Environment.NewLine + System.Environment.NewLine +
                                ex.StackTrace;
                MessageBox.Show(message1, caption, MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }

            if (!String.IsNullOrEmpty(message))
            {
                SimpleLogWriter.WriteLogLine("--- + MAIN FORM NAVIGATION ERROR + ---");
                SimpleLogWriter.WriteLogLine(message);
                SimpleLogWriter.WriteLogLine("--- - MAIN FORM NAVIGATION ERROR - ---");
            }

            if (!String.IsNullOrEmpty(message1))
            {
                SimpleLogWriter.WriteLogLine("--- + SEVERE ERROR + ---");
                SimpleLogWriter.WriteLogLine(message1);
                SimpleLogWriter.WriteLogLine("--- - SEVERE ERROR - ---");
            }
        }

    }
}
